package Day013_线程安全.demo01_线程池;

import java.util.concurrent.*;

/**
 * @author zhangyan
 * @date 2020/5/4 10:29
 */
public class Test1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService es = Executors.newFixedThreadPool(2);
        //创建任务
        MyCallable1 mc1 = new MyCallable1(100);
        MyCallable1 mc2 = new MyCallable1(10);
        //提交并执行任务
        Future<Integer> future1 = es.submit(mc1);
        Future<Integer> future2 = es.submit(mc2);

        System.out.println(future1.get());
        System.out.println(future2.get());
    }
}

class MyCallable1 implements Callable<Integer>{
    //成员变量
    int n;

    public MyCallable1(int n) {
        this.n = n;
    }

    @Override
    public Integer call() {
        //任务: 求1-n的和,并返回
        //call是无参方法,需要定义成员变量n
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }
}